<?php
namespace app\modules\api\models;

use yii;
use yii\db\Query;

class Inquiry 
{   
    private $table;

    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix.'inquiry';
    }

    /**
     * @return array
     */
    public function list($p,$s,$where=[]):array
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if ($where['star']) {
            $db->andWhere(['star'=>$where['star']]);
        }
        if ($where['recommend']) {
            $db->andWhere(['recommend'=>$where['recommend']]);
        }
        if ($where['status']) {
            $db->andWhere(['status'=>$where['status']]);
        }
        if ($where['property']) {
            $db->andWhere(['property'=>$where['property']]);
        }
        if ($where['service_name']) {
            $db->andWhere(['service_name'=>$where['service_name']]);
        }
        if ($where['begin_time']) {
            $db->andWhere(['>=','consultation_time',strtotime($where['begin_time'])]);
        }
        if ($where['end_time']) {
            $db->andWhere(['<=','consultation_time',(strtotime($where['end_time'])+3600*24)]); 
        }
        if ($where['email']) {
            $db->andWhere(['email'=>$where['email']]);
        }
        if ($where['name']) {
            $db->andWhere(['like','name',$where['name']]);
        }
        if ($where['inquiry_detail']) {
            $db->andWhere(['like','inquiry_detail',$where['inquiry_detail']]);
        }
        return $db->orderBy(['consultation_time'=>SORT_DESC,'id'=>SORT_DESC])
        ->offset(($p-1)*$s)
        ->limit($s)
        ->all();

    }

    /**
     * @return int 
     */
    public function size($where = []):int
    {
        $db = new Query();
        $db->select('count(*) as num')->from($this->table);
        if ($where['star']) {
            $db->andWhere(['star'=>$where['star']]);
        }
        if ($where['recommend']) {
            $db->andWhere(['recommend'=>$where['recommend']]);
        }
        if ($where['status']) {
            $db->andWhere(['status'=>$where['status']]);
        }
        if ($where['property']) {
            $db->andWhere(['property'=>$where['property']]);
        }
        if ($where['service_name']) {
            $db->andWhere(['service_name'=>$where['service_name']]);
        }
        if ($where['begin_time']) {
            $db->andWhere(['>=','consultation_time',strtotime($where['begin_time'])]);
        }
        if ($where['end_time']) {
            $db->andWhere(['<=','consultation_time',(strtotime($where['end_time'])+3600*24)]); 
        }
        if ($where['email']) {
            $db->andWhere(['email'=>$where['email']]);
        }
        if ($where['name']) {
            $db->andWhere(['like','name',$where['name']]);
        }
        if ($where['inquiry_detail']) {
            $db->andWhere(['like','inquiry_detail',$where['inquiry_detail']]);
        }
        $re = $db->one();
        return $re?$re['num']:0;
    }
}